package com.group1.bookmanagement.domain;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import com.group1.bookmanagement.util.DateUtils;
import com.group1.bookmanagement.util.PhotoUtils;
import com.group1.bookmanagement.util.RandomCharacter;

/**
 * 图片
 */
@Entity
@Table(name = "photo")
public class Photo {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	// 图片文件
	@Transient
	private File file;
	// 图片原名称
	private String oldName;
	// 图片新名称
	private String newName;
	// 保存路径
	private String path;
	// 地址
	private String url;
	// 大小
	private Double size;
	// 单位
	private String unit;
	// 后缀名
	private String suffix;
	// 创建时间
	private Date createdAt;
	// 修改时间
	private Date updatedAt;

	public Photo() {
		Date date = new Date();
		createdAt = date;
		updatedAt = date;
	}

	public Photo(File file) {
		setFile(file);
		Date date = new Date();
		createdAt = date;
		updatedAt = date;
	}

	public void upload(HttpServletRequest request) {
		if (file != null) {
			path = new StringBuilder(request.getSession().getServletContext().getRealPath("/upload/"))
					.append(DateUtils.getCurrentYear())
					.append(File.separator)
					.append(DateUtils.getCurrentMonth())
					.append(File.separator)
					.append(DateUtils.getCurrentDay())
					.append(File.separator)
					.append(newName)
					.toString();
			
			url = new StringBuilder("http://localhost:8080/BookManagement/upload/")
					.append(DateUtils.getCurrentYear())
					.append("/")
					.append(DateUtils.getCurrentMonth())
					.append("/")
					.append(DateUtils.getCurrentDay())
					.append("/")
					.append(newName)
					.toString();
			
			try {
				FileUtils.copyFile(file, new File(this.path));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public File getFile() {
		if (path == null) {
			return null;
		}
		
		return new File(path);
	}

	public void setFile(File file) {
		this.file = file;
//		oldName = file.getName();
		String fileName = file.getName();
		oldName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "jpg";
		newName = RandomCharacter.randomName(oldName);
		Map map = PhotoUtils.convert(file.length());
		size = (Double) map.get(PhotoUtils.SIZE);
		unit = (String) map.get(PhotoUtils.UNIT);
		suffix = oldName.substring(oldName.lastIndexOf(".") + 1);
	}

	public String getOldName() {
		return oldName;
	}

	public void setOldName(String oldName) {
		this.oldName = oldName;
	}

	public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Double getSize() {
		return size;
	}

	public void setSize(Double size) {
		this.size = size;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public Date getUpdatedAt() {
		return updatedAt;
	}

	public void setUpdatedAt(Date updatedAt) {
		this.updatedAt = updatedAt;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		
		if (!(obj instanceof Label)) {
			return false;
		}
		
		Photo photo = (Photo) obj;
		return this.getId().equals(photo.getId());
	}
}
